package com.fdb.efp.flow.api.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import com.fdb.efp.flow.service.facade.InstFlowEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fdb.efp.flow.service.facade.FlowWholeLifeCycleService;
import com.fdb.efp.flow.service.facade.InstFlowProcessService;
import com.fdb.efp.flow.service.facade.InstFlowTaskService;
import com.fdb.efp.flow.service.vo.FlowInstanceInfoVO;
import com.fdb.efp.flow.service.vo.InstFlowEventVO;
import com.fdb.efp.flow.service.vo.InstFlowProcessVO;
import com.fdb.efp.flow.service.vo.InstFlowTaskVO;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.basic.framework.web.controller.IController;

@RestController
@RequestMapping("/api")
public class FlowWholeLifeCycleController extends AbstractController implements IController {
    
    @Autowired
    @Qualifier("flowWholeLifeCycleService")
    private FlowWholeLifeCycleService flowWholeLifeCycleService;
    
    @Autowired
    @Qualifier("instFlowProcessService")
    private InstFlowProcessService instFlowProcessService;
    
    @Autowired
    @Qualifier("instFlowTaskService")
    private InstFlowTaskService instFlowTaskService;

    @Autowired
    @Qualifier("instFlowEventService")
    private InstFlowEventService instFlowEventService;
    
    @RequestMapping(value="/flow/instance/infos", method=RequestMethod.POST)
    public @ResponseBody ResponseData<List<FlowInstanceInfoVO>> queryFlowInstanceInfos(@RequestBody FlowInstanceInfoVO vo) {
        //vo = setUserInfoToVO(vo);
        List<FlowInstanceInfoVO> vos = null;
        try {
            vos = flowWholeLifeCycleService.queryAllCurrLegalOrgDown(vo);
        } catch (Exception e) {
            
        }
        return getResponseData(vos);
    }

    @RequestMapping(value="/stage/events", method=RequestMethod.GET)
    public @ResponseBody ResponseData<List<Map<String, String>>> queryStageEvents(@RequestParam("instFlowId") String instFlowId) {
        List<Map<String, String>> treeNodes = null;
        try {
            List<InstFlowEventVO> vos = flowWholeLifeCycleService.queryStageEventTree(instFlowId);
            treeNodes = new ArrayList<Map<String, String>>();
            if (Objects.nonNull(vos) && !vos.isEmpty()) {
                Set<String> stages = new HashSet<String>();
                for (InstFlowEventVO vo : vos) {
                    if (!stages.contains(vo.getBizStageId())) {
                        stages.add(vo.getBizStageId());
                        Map<String, String> parentNode = new HashMap<String, String>();
                        parentNode.put("treeNodeId", vo.getBizStageId());
                        parentNode.put("treeNodeName", vo.getBizStageName());
                        parentNode.put("treeParentId", "000000");
                        treeNodes.add(parentNode);
                    }
                    Map<String, String> node = new HashMap<String, String>();
                    node.put("treeNodeId", vo.getBizSerno());
                    node.put("treeNodeName", vo.getBizEventName());
                    node.put("treeParentId", vo.getBizStageId());
                    treeNodes.add(node);
                }
            }
            
        } catch (Exception e) {
            
        }
        return getResponseData(treeNodes);
    }
    
    @RequestMapping(value="/event/nodes", method=RequestMethod.GET)
    public @ResponseBody ResponseData<List<InstFlowProcessVO>> queryAllNodesByBizSerno(@RequestParam("bizSerno") String bizSerno) {
        List<InstFlowProcessVO> vos = instFlowProcessService.queryAllByBizSerno(bizSerno);
        
        return getResponseData(vos);
    }
    
    @RequestMapping(value="/event/node/tasks", method=RequestMethod.GET)
    public @ResponseBody ResponseData<List<InstFlowTaskVO>> queryAllTasksByInstNodeId(@RequestParam("instNodeId") String nodeInstanceId) {
        List<InstFlowTaskVO> vos = instFlowTaskService.queryByInstNodeId(nodeInstanceId);
        return getResponseData(vos);
    }

    /**
     * ��ѯ��ͣ������ֹ������ʵ��
     * @param inVo
     * @return
     */
    @RequestMapping(value = "/flow/event/info/terminateOrPause",method = RequestMethod.POST)
    public @ResponseBody
    ResponseData<List<InstFlowEventVO>> queryTerminateAndPause(@RequestBody InstFlowEventVO inVo){
        List<InstFlowEventVO> list = instFlowEventService.queryTerminateAndPasueByPage(inVo);
        return getResponseData(list);
    }
}
